package com.shixj.easy.spider.service;

import com.shixj.easy.spider.mapper.ChapterMapper;
import com.shixj.easy.spider.mapper.NovelMapper;
import com.shixj.easy.spider.mapper.ViewRecordMapper;
import com.shixj.easy.spider.model.Catalog;
import com.shixj.easy.spider.model.Chapter;
import com.shixj.easy.spider.model.Novel;
import com.shixj.easy.spider.model.ViewRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.utils.page.PageUtil;
import java.util.List;

/**
 * Created by shixj on 2018/1/16.
 * 异步
 */
@Service
public class ReadServiceImpl implements ReadService {
    @Autowired
    ChapterMapper chapterMapper;
    @Autowired
    NovelMapper novelMapper;
    @Autowired
    ViewRecordMapper viewRecordMapper;

    @Override
    @Transactional
    public List<Novel> getNovelList(int num, int size, Novel query, Long guestId) {
        PageUtil.start(num, size);
        List<Novel> novelList = novelMapper.getNovelList(query);
        novelList.stream().forEach(novel ->
                novel.setCurrentChapter(getCurrentChapter(guestId, novel.getId()))
        );
        return novelList;
    }

    /**
     * 获取小说章节内容
     *
     * @param novelId
     * @param sourceId
     * @return
     */
    @Override
    @Transactional
    public Chapter read(Long guestId, Long novelId, Long sourceId,Long maxSourceId)throws Exception {
        Chapter chapter;
        Chapter query=new Chapter();
        query.setSourceId(String.valueOf(sourceId));
        query.setNovelId(novelId);
        List<Chapter> chapterList=chapterMapper.select(query);
        if (chapterList != null && !chapterList.isEmpty())
            chapter= chapterList.get(0);
         else{
            if (maxSourceId.equals(0L))
                maxSourceId=chapterMapper.getMaxSourceId(novelId);
                if(sourceId<=maxSourceId)
                    chapter= chapterMapper.getNextChapter(query);
                else throw new  Exception("没有更多章节！");
        }
        ViewRecord viewRecord = getViewRecord(guestId, novelId);
        if (!viewRecord.getSourceId().equals(Long.valueOf(chapter.getSourceId()))) {
            viewRecord.setSourceId(Long.valueOf(chapter.getSourceId()));
            viewRecordMapper.updateByPrimaryKey(viewRecord);
        }
        return  chapter;
    }

    /**
     * 获取小说章节列表
     *
     * @param novelId
     * @return
     */
    @Override
    @Transactional
    @Deprecated
    public Catalog readList(Long novelId) {
        Novel query=new Novel();
        query.setId(novelId);
        Catalog catalog=new Catalog();
        catalog.setChapterList(chapterMapper.getChapterListAsc(query));
        catalog.setNovel(novelMapper.selectOne(query));
        return catalog;
    }

    /**
     * 根据ID获取小说
     *
     * @param chapter
     * @return
     */
    @Override
    public Novel getNovelById(Chapter chapter) {
        Novel novel = novelMapper.selectByPrimaryKey(chapter.getNovelId());
        //novel.setMaxSourceId(chapterMapper.getMaxSourceId(chapter.getNovelId()));
        novel.setCount(chapterMapper.getSourceConut(chapter));
        return novel;
    }

    /**
     * 获取小说章节列表（分页）
     *
     * @param novelId
     * @param num
     * @param size
     * @return
     */
    @Override
    @Transactional
    public List<Chapter> readList(Long guestId, Long novelId, int num, int size) {
        getViewRecord(guestId, novelId);
        Novel query = new Novel();
        query.setId(novelId);
        PageUtil.start(num, size);
        return chapterMapper.getChapterListAsc(query);
    }

    /**
     * 获取上次阅读章节
     * 没有开始阅读的返回第一章
     *
     * @param guestId
     * @param novelId
     * @return
     */
    @Override
    @Transactional
    public Chapter getCurrentChapter(Long guestId, Long novelId) {
        ViewRecord viewRecord = getViewRecord(guestId, novelId);
        Chapter chapterQ = new Chapter();
        chapterQ.setNovelId(novelId);
        chapterQ.setSourceId(String.valueOf(viewRecord.getSourceId()));
        Chapter chapter = chapterMapper.getChapterNOContent(chapterQ);
        if (chapter == null) {
            viewRecord.setSourceId(viewRecord.getSourceId() + 1);
            viewRecordMapper.updateByPrimaryKey(viewRecord);
            return getCurrentChapter(guestId, novelId);
        } else
            return chapter;
    }

    private ViewRecord getViewRecord(Long guestId, Long novelId) {
        ViewRecord queryV = new ViewRecord();
        queryV.setNovelId(novelId);
        queryV.setGuestId(guestId);
        ViewRecord viewRecord = viewRecordMapper.selectOne(queryV);
        if (viewRecord == null) {
            queryV.setSourceId(chapterMapper.getMinSourceId(novelId));
            viewRecordMapper.insert(queryV);
            return queryV;
        }
        return viewRecord;
    }
}
